/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zjdt.epserver.db.dao4mysql;

import com.zjdt.epserver.db.dao.DriverDao;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 *
 * @author dusin
 */
public class DriverDaoImpl implements DriverDao {

    static Logger logger = Logger.getLogger(DriverDaoImpl.class);
    private JdbcTemplate jdbcTemplate;

    public DriverDaoImpl(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public boolean doCreate(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "insert into driver(username,password,name,mobilePhone,license,licenseNo,duedate,siterID) values(?,?,?,?,?,?,?,?)";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doDeleteById(int id) {
        boolean flag = true;
        String sql = null;
        sql = "delete from driver where id=?";
        int rows = jdbcTemplate.update(sql, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateById(Object[] bean) {
        boolean flag = true;
        String sql = null;
        sql = "update driver set username=?,name=?,mobilePhone=?,licenseNo=?,duedate=? where id=?";
        int rows = jdbcTemplate.update(sql, bean);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdatePasswordByID(int id, String password) {
        boolean flag = true;
        String sql = null;
        sql = "update driver set password=? where id=?";
        int rows = jdbcTemplate.update(sql, password, id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public boolean doUpdatePasswordByUsername(String username, String password) {
        boolean flag = true;
        String sql = null;
        sql = "update driver set password=? where username=?";
        int rows = jdbcTemplate.update(sql, password, username);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public Map<String, Object> findById(int id) {
        String sql = null;
        try {
            sql = "select * from  driver where id = ?";
            return jdbcTemplate.queryForMap(sql, id);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> findByMobilePhone(String mobilePhone) {
        String sql = null;
        try {
            sql = "select * from  driver where mobilePhone = ?";
            return jdbcTemplate.queryForMap(sql, mobilePhone);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
    
    @Override
    public List<Map<String, Object>> findAll() {
        String sql = "select * from driver";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<Map<String, Object>> findAll(String key) {
        String sql = "select * from driver where name like ? or mobilePhone like ?";
        return jdbcTemplate.queryForList(sql, "%" + key + "%", "%" + key + "%");
    }
    
     @Override
    public List<Map<String, Object>> findAll(int status) {
        String sql = "select * from driver where status= ?";
        return jdbcTemplate.queryForList(sql, status);
    }
    
    @Override
    public List<Map<String, Object>> findAll(String key,int status) {
        String sql = "select * from driver where (name like ? or mobilePhone like ?,or licenseNo like ?) and status=?";
        return jdbcTemplate.queryForList(sql, "%" + key + "%", "%" + key + "%","%" + key + "%",status);
    }

    @Override
    public List<Map<String, Object>> findAllTruck(int id) {
        String sql = "select * from truck where driverId = ?";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public Map<String, Object> login(String username, String password) {
        String sql;
        sql = "select * from  driver where username = ?  and password=?";
        logger.info(sql+"?["+username+","+password+"]");
        Map<String, Object> driver = null;
        try {
            driver = jdbcTemplate.queryForMap(sql, username, password);
        } catch (Exception e) {
        }
        return driver;
    }

    public boolean doUpdateLicensePhotoById(int id,String license) {
        boolean flag = true;
        String sql = null;
        sql = "update driver set license=? where id=?";
        int rows = jdbcTemplate.update(sql,license,id );
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    
    @Override
    public boolean doUpdateLicensePhotoByUsername(String username,String dueDate,String license) {
        boolean flag = true;
        String sql = null;
        sql = "update driver set duedate=?,license=? where username=?";
        int rows = jdbcTemplate.update(sql,dueDate,license,username );
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doUpdateStatusById(int id, int status) {
        boolean flag = true;
        String sql = null;
        sql = "update driver set status=? where id=?";
        int rows = jdbcTemplate.update(sql, status,id);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
    public boolean doCreateTrip(String rid, String startTime, int driverId, String plateNo, String fcode, BigDecimal longitude,BigDecimal latitude) {
        boolean flag = true;
        String sql = null;
        sql = "insert into atrip(rid,startTime,driverId,plateNo,fcode,longitude,latitude) values(?,?,?,?,?,?,?)";
        logger.info(sql+"?"+Arrays.asList(new Object[]{rid,startTime,driverId,plateNo,fcode,longitude,latitude}));
        int rows = jdbcTemplate.update(sql, rid,startTime,driverId,plateNo,fcode,longitude,latitude);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    @Override
    public boolean doUpdateTripLocation(String rid, BigDecimal longitude,BigDecimal latitude,String time,String stopTime,int status) {
        boolean flag = true;
        String sql = null;
        sql = "update atrip set longitude=?,latitude=?,time=?,stopTime=?,status=? where rid=?";
        int rows = jdbcTemplate.update(sql, longitude,latitude,time,stopTime,status,rid);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
    @Override
    public boolean doDeleteTrip(String  rid) {
        boolean flag = true;
        String sql = null;
        sql = "delete from atrip where rid=?";
        int rows = jdbcTemplate.update(sql, rid);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }

    @Override
     public List<Map<String,Object>> findAllTrip(int id,String date){
         String sql = "select * from vtrip where (driverid = ? and startTime>?)";
         logger.info(sql+"?["+id+","+date+"]");
         return jdbcTemplate.queryForList(sql, id,date);
     }
     
     @Override
     public List<Map<String,Object>> findAllHistoryTrip(int id,String date){
         String sql = "select * from vtrip where (driverid = ? and startTime>? and (status=9 or status=2)) order by startTime desc";
         logger.info(sql+"?["+id+","+date+"]");
         return jdbcTemplate.queryForList(sql, id,date);
     }
    
     @Override
     public List<Map<String,Object>> findIncomingTruck(String factoryCode,String date){
         String sql = "select * from vtrip where (fCode = ? and startTime>? and status =1)";
         logger.info(sql+"?["+factoryCode+","+date+"]");
         return jdbcTemplate.queryForList(sql, factoryCode,date);
     }
     
     //查找已到达，未处理的卡车
    @Override
     public List<Map<String,Object>> findReachedTruck(String factoryCode,String date){
         String sql = "select * from atrip where (fCode = ? and startTime>? and status=2)";
         logger.info(sql+"?["+factoryCode+","+date+"]");
         return jdbcTemplate.queryForList(sql, factoryCode,date);
     }
     
    @Override
    public boolean doCreateLocation(String rid, BigDecimal longitude, BigDecimal latitude, String time,int status) {
        boolean flag = true;
        String sql = null;
        sql = "insert into location(tripRid,longitude,latitude,time,status) values(?,?,?,?,?)";
        logger.info(sql+Arrays.asList(new Object[]{rid,longitude,latitude,time,status}));
        int rows = jdbcTemplate.update(sql, rid,longitude,latitude,time,status);
        if (rows <= 0) {
            flag = false;
        }
        return flag;
    }
}
